package com.example.lsn_opengles713;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;

import java.lang.reflect.Field;

public class MainActivity extends Activity {

	//
	private AbstractMyRenderer render;
	private MyGLSurfaceView view;
	private Dialog d;
	private Resources r;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		r = this.getResources();
		view = new MyGLSurfaceView(this);
		render = new MyColorCubeRenderer();
		// view.setEGLConfigChooser(5,6,5,0,8, 8);//
		view.setRenderer(render);
		// GLSurfaceView.RENDERMODE_CONTINUOUSLY:持续渲染(默认)
		// GLSurfaceView.RENDERMODE_WHEN_DIRTY:脏渲染,命令渲染
		view.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
		setContentView(view);
		buildDialog();
	}

	private void buildDialog() {
		d = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		d.setContentView(R.layout.light_setting);
		addCheckboxEvent();
		
	}
	//添加复选框事件
	private void addCheckboxEvent() {
		//box监听
		CompoundButton.OnCheckedChangeListener boxl = new  CompoundButton.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView,	boolean isChecked) {
				try {
					int id = buttonView.getId();
					String name = r.getResourceName(id);
					name = name.substring(name.lastIndexOf("_") + 1);
					Class clazz = render.getClass();
					Field f = clazz.getField(name);
					f.setAccessible(true);
					f.set(render, isChecked);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
		
		/**
		 * 进度改变
		 */
		SeekBar.OnSeekBarChangeListener barl = new SeekBar.OnSeekBarChangeListener(){
			public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
				try {
					float scale = (float)progress / (float)seekBar.getMax();
					int id = seekBar.getId();
					String name = r.getResourceName(id);
					name = name.substring(name.lastIndexOf("_") + 1);
					Class clazz = render.getClass();
					Field f = clazz.getField(name);
					f.setAccessible(true);
					if(name.contains("pos")){
						scale = -10f + (20 * scale) ;
					}
					f.set(render, scale);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		};
		try {
			Class clazz = R.id.class ;
			Field[] fs = clazz.getDeclaredFields();
			CheckBox box = null ;
			SeekBar bar = null ;
			for(Field f : fs){
				int id = (Integer)f.get(null);
				View view = d.findViewById(id);
				if(view != null){
					if(view instanceof CheckBox){
						box = (CheckBox) view ;
						box.setOnCheckedChangeListener(boxl);
					}
					else if(view instanceof SeekBar){
						bar = (SeekBar) view ;
						bar.setOnSeekBarChangeListener(barl);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class MyGLSurfaceView extends GLSurfaceView {
		public MyGLSurfaceView(Context context) {
			super(context);
		}

		public MyGLSurfaceView(Context context, AttributeSet attrs) {
			super(context, attrs);
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		float step = 5f;
		// up
		if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
			render.xrotate = render.xrotate - step;
		} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
			render.xrotate = render.xrotate + step;
			// 请求渲染,和脏渲染配合使用
			view.requestRender();
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
			render.yrotate = render.yrotate - step;
			// 请求渲染,和脏渲染配合使用
			view.requestRender();
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
			render.yrotate = render.yrotate + step;
			// 请求渲染,和脏渲染配合使用
			view.requestRender();
			return true;
		}
		// 请求渲染,和脏渲染配合使用
		view.requestRender();
		return super.onKeyDown(keyCode, event);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("设置");
		return super.onCreateOptionsMenu(menu);
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		d.show();
		return super.onOptionsItemSelected(item);
	}
}